package com.cloudm.technician.ui.modules.cloudm_box.debug_result

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DebugResultItemInfo
import com.cloudm.technician.extensions.inflate
import com.cloudm.technician.extensions.logger
import kotlinx.android.synthetic.main.item_debug_result.view.*

/**
 * 作者: wesley
 * 时间: 2017/12/22
 * 描述: 适配器
 */
class DebugResultAdapter : RecyclerView.Adapter<DebugResultAdapter.ItemViewHolder>() {

    private val list: ArrayList<DebugResultItemInfo> = arrayListOf()


    fun addAllItems(info: ArrayList<DebugResultItemInfo>?) {
        info?.let {
            list.clear()
            list.addAll(info)
            notifyDataSetChanged()
        }
    }

    override fun onBindViewHolder(holder: ItemViewHolder?, position: Int) {
        if (holder == null || list.size <= position) {
            logger(" DebugResultAdapter position = $position list.size = ${list.size}")
            return
        }
        val info = list[position]
        holder.itemView.tvItemKey.text = info.name
        holder.itemView.tvItemValue.text = StringBuffer().append(info.range ?: "").append(
                " ").append(info.value ?: "")

        when (info.isPass) {
            1 -> holder.itemView.ivItemStatus.setImageResource(R.drawable.ic_done_blue_24px)
            2 -> holder.itemView.ivItemStatus.setImageResource(
                    R.drawable.ic_priority_high_white_24px)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ItemViewHolder {
        return ItemViewHolder(parent?.inflate(R.layout.item_debug_result))
    }

    override fun getItemCount(): Int = list.size

    class ItemViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)
}